const { PrismaClient } = require('@prisma/client')
const bcrypt = require('bcryptjs')

const prisma = new PrismaClient()

async function main() {
  console.log('开始初始化数据库...')

  try {
    // 创建默认管理员用户
    const hashedPassword = await bcrypt.hash('admin', 12)
    
    const adminUser = await prisma.user.upsert({
      where: { email: 'admin@example.com' },
      update: {},
      create: {
        email: 'admin@example.com',
        username: 'admin',
        password: hashedPassword,
        role: 'admin'
      }
    })

    console.log('✅ 默认管理员用户创建成功:')
    console.log('   邮箱: admin@example.com')
    console.log('   密码: admin')
    console.log('   用户名: admin')

    // 创建示例视频数据
    const sampleVideo = await prisma.tikTokVideo.upsert({
      where: { videoId: 'sample_video_001' },
      update: {},
      create: {
        videoId: 'sample_video_001',
        url: 'https://www.tiktok.com/@example/video/sample_video_001',
        title: '示例TikTok视频',
        description: '这是一个示例视频，用于演示系统功能',
        author: 'example_user',
        authorId: 'example_user_id',
        likes: 1000,
        shares: 100,
        comments: 50,
        views: 10000,
        duration: 30
      }
    })

    console.log('✅ 示例视频数据创建成功')

    // 创建示例文案提取记录
    const sampleExtraction = await prisma.textExtraction.upsert({
      where: { id: 1 },
      update: {},
      create: {
        videoId: sampleVideo.id,
        extractedText: '这是从示例视频中提取的文案内容，展示了系统的文案提取功能。',
        confidence: 0.95,
        language: 'zh-CN',
        method: 'OCR',
        status: 'completed'
      }
    })

    console.log('✅ 示例文案提取记录创建成功')
    console.log('🎉 数据库初始化完成！')

  } catch (error) {
    console.error('❌ 数据库初始化失败:', error)
    throw error
  }
}

main()
  .catch((e) => {
    console.error(e)
    process.exit(1)
  })
  .finally(async () => {
    await prisma.$disconnect()
  })
